#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //设置列数
    ui->treeWidget->setColumnCount(3);
    // ui->treeWidget->resize(600,300);
    //添加顶层结点
    QTreeWidgetItem *topItem = new QTreeWidgetItem;
    topItem->setText(0,"教程");
    topItem->setCheckState(0,Qt::Unchecked);
    ui->treeWidget->addTopLevelItem(topItem);
    //添加子结点
    QStringList c;
    c << "C语言教程" << "http://c.biancheng.net/c/" <<"已完成";
    QTreeWidgetItem *childItem1 = new QTreeWidgetItem(topItem,c);
    childItem1->setCheckState(0,Qt::Unchecked);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_treeWidget_itemClicked(QTreeWidgetItem *item, int column)
{
    qDebug()<< item->text(column);
}

void Widget::on_treeAdd_clicked()
{
    QTreeWidgetItem *selItem = ui->treeWidget->currentItem();

    qDebug()<< (selItem==nullptr);

    AddDialog *addDialog = new AddDialog(nullptr, selItem);
    connect(addDialog, &AddDialog::doneAddItem, this, &Widget::addTreeItem);
    addDialog->show();


}

void Widget::addTreeItem(QTreeWidgetItem *item, QString text)
{

    if (nullptr == item) {
        QTreeWidgetItem *topItem = new QTreeWidgetItem;
        topItem->setText(0, text);
        topItem->setCheckState(0,Qt::Unchecked);
        ui->treeWidget->addTopLevelItem(topItem);
    }else {
        //添加子结点
        QStringList c;
        c << text << "http://c.biancheng.net/c/" <<"已完成";
        QTreeWidgetItem *childItem1 = new QTreeWidgetItem(item, c);
        childItem1->setCheckState(0,Qt::Unchecked);
    }
    qDebug()<< "aa";
    qDebug()<< text;
}
